﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using XDerive.XStudio.Support.SupportAppI.User;

namespace WebStudio.Mvc.Core.Filter
{
    /// <summary>
    /// 权限过滤器
    /// </summary>
    public class PowerFilter : FilterAttribute, IAuthorizationFilter
    {
        /// <summary>
        /// 模块标识
        /// </summary>
        public int PartID { get; set; }

        /// <summary>
        /// 模块标识列表
        /// </summary>
        public int[] Parts { get; set; }

        public void OnAuthorization(AuthorizationContext filterContext)
        {
            bool isCheck = false;
            if (PartID > 0)
            {
                isCheck = isCheck || UserAppI.CheckPower(PartID);
            }
            if (!isCheck && Parts != null && Parts.Length > 0)
            {
                foreach (var item in Parts)
                {
                    isCheck = isCheck || UserAppI.CheckPower(item);
                }
            }

            if (!isCheck)
            {
                Do(filterContext);
            }
        }


        private void Do(AuthorizationContext filterContext)
        {
            ContentResult content = new ContentResult();
            content.Content = "您没有权限访问此页面！";
            content.ContentEncoding = Encoding.UTF8;
            filterContext.Result = content;
        }

    }

}
